// miniprogram/pages/card/list/list.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isEmpty: false,
    cards: [],
    show: false,
    selectedIndex: '',
    total: 0,
    page: 1,
    pageSize: 20,
    dialogName:"",
    actions: [
      {
        name: '审核通过',
      },
      {
        name: '不予通过',
      }
    ],
    hiddenModal: true,
    point:0,
    comment: "",
    checkStatus: ''
  },

  onClose() {
    this.setData({ show: false });
  },
  getUnCheckedList() {
    this.setData({
      page: 1,
      pagesize: 20,
    });

    const db = wx.cloud.database();
    const _ = db.command;
    db.collection('cards').orderBy('systemTime', 'desc').limit(20).where({
      status: _.eq('unChecked')
    }).get({
      success: res => {
        console.log('cards',res.data);
        if (res.data.length == 0) {
          this.setData({ isEmpty: true })
        } else {
          this.setData({ isEmpty: false })
        }
        this.setData({ cards: res.data })
      }
    });
  },
  onClickPreviewImg: function (e) {
    let card = e.currentTarget.dataset.card
    let index = e.currentTarget.dataset.index
    wx.previewImage({
      current:card.images[index],
      urls: card.images,
    })
  },
  onSelect(event) {
    console.log(event.detail);
    this.setData({
      hiddenModal: !this.data.hiddenModal
    })
    this.setData({

    })
    switch (event.detail.name) {
      case '审核通过':
        this.setData({
          checkStatus: 'checked',
          dialogName:'审核通过',
          comment: '',
          point:0
        })
        break
      case '不予通过':
        this.setData({
          checkStatus: 'rejected',
          dialogName:'驳回申请',
          comment: '',
          point:0
        })
        break

    }
  },
  onClickUser: function (e) {
    var card = e.currentTarget.dataset.card
    wx.navigateTo({
      url: '../../userDetail/detail?openid=' + card._openid + '&nickName=' + card.userInfo.nickName + '&avatarUrl=' + card.userInfo.avatarUrl,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUnCheckedList()
  },


  onClickApprove: function (e) {
    console.log('onClickApprove', e)
    this.setData({ show: true });
    this.setData({ selectedIndex: e.currentTarget.dataset.index });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      pagesize: 20,
    });

    const db = wx.cloud.database();
    const _ = db.command;
    let openid = wx.getStorageSync('openid')
    console.log(openid)
    wx.showNavigationBarLoading() //在标题栏中显示加载
    db.collection('cards').orderBy('systemTime', 'desc').limit(20).where({
      status: _.eq('unChecked')
    }).get({
      success: res => {
        console.log(res.data);
        if (res.data.length == 0) {
          this.setData({ isEmpty: true })
        } else {
          this.setData({ isEmpty: false })
        }
        this.setData({ cards: res.data })
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.data.page++
    console.log("skip:", this.data.page)

    this.setData({
      page: this.data.page,
      pagesize: 20
    });

    const db = wx.cloud.database();
    const _ = db.command;
    let skip = (this.data.page - 1) * 20
    console.log("skip:", skip)
    db.collection('cards').skip(skip).orderBy('systemTime', 'desc').limit(20).where({
      status: _.eq('unChecked')
    }).get({
      success: res => {
        this.setData({ cards: this.data.cards.concat(res.data) })
      }
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  confirmM: function () {
    this.setData({
      hiddenModal: true
    })
    console.log("意见" + this.data.comment);
    wx.showLoading({
      title: '审核中...',
    })
    let avatarUrl = wx.getStorageSync('avatarUrl');
    let nickName = wx.getStorageSync('nickName');
    
    wx.cloud.callFunction({
      name: 'approveCard',
      data: {
        id: this.data.selectedIndex,
        status: this.data.checkStatus,
        extraPoint:this.data.point,
        comment: {
          commentAvatarUrl: avatarUrl,
          commentNickName: nickName,
          content: this.data.comment
        }
      },
      success: res => {
        wx.hideLoading()
        if (res.result.success) {
          
          wx.showToast({
            title: '审核成功',
            icon: 'success',
            duration: 2000
          })
          wx.cloud.callFunction({
            name: 'handleUserWeekPoint',
            data: {},
            success: res => {
              console.log('云函数handleUserWeekPoint调用成功');
            }
          })  // 处理周积分
          this.getUnCheckedList()
        } else {
          wx.showToast({
            title: '审核失败',
            icon: 'success',
            duration: 2000
          })
        }

      },
      fail: err => {
        wx.hideLoading()
      }
    })
  },

  cancelM: function () {
    this.setData({
      hiddenModal: true,
      comment: ''
    })
  },

  comment: function (e) {
    this.setData({
      comment: e.detail.value
    })
  },
})


